package com.wuyi.controller;

import com.wuyi.pojo.Resource;
import com.wuyi.pojo.bo.ResourceBO;
import com.wuyi.pojo.bo.ResourcesBO;
import com.wuyi.service.FileUploadService;
import com.wuyi.service.ResourceService;
import com.zc.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName ResourceController.java
 * @Description 资源方面的接口
 * @createTime 2021年10月19日 16:54:00
 */
@Api(value = "资源",tags = {"用于资源的相关接口"})
@RestController
@RequestMapping("resource")
public class ResourceController {

    public final static Logger logger = LoggerFactory.getLogger(ResourceController.class);

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private FileUploadService fileUploadService;


    /**
     * 首页获取10条资源列表，优先级和发布时间排序
     * @return
     */
    @ApiOperation(value = "获取资源列表（可调试）", notes = "获取资源列表（可调试）", httpMethod = "GET")
    @GetMapping("/list")
    public JSONResult list() {
        List<Resource> list = resourceService.list();
        return JSONResult.ok(list);
    }


    @ApiOperation(value = "根据条件查询资源信息", notes = "根据条件查询资源信息", httpMethod = "GET")
    @GetMapping("/queryResInfo")
    public JSONResult queryResInfo(@RequestBody ResourceBO resourceBO) {
        List<Resource> list = resourceService.queryResInfo(resourceBO);
        return JSONResult.ok(list);
    }

    @ApiOperation(value = "新增资源（已对接）", notes = "新增资源（已对接）", httpMethod = "POST")
    @PostMapping("/addResource")
    public JSONResult addResource(@ApiParam(name = "resourceName", value = "资源名称", required = true)
                          @RequestParam String resourceName,
                          @ApiParam(name = "resourceCid", value = "类目", required = true)
                          @RequestParam Integer resourceCid,
                          @ApiParam(name = "userName", value = "上传的用户名", required = true)
                          @RequestParam String userName,
                          @ApiParam(name = "resourceDesc", value = "资源描述", required = true)
                          @RequestParam String resourceDesc,
                          @ApiParam(name = "coverImg", value = "封面图片", required = false)
                          @RequestParam("coverImg") MultipartFile coverImg,
                          @ApiParam(name = "files", value = "资源文件", required = true)
                          @RequestParam("files") MultipartFile[] files) throws IOException {

        //判空操作
        if (StringUtils.isBlank(resourceName)) {
            return JSONResult.errorMsg("资源名称不能为空");
        }
        if (null == resourceCid) {
            return JSONResult.errorMsg("资源类目不能为空");
        }
        if (files.length < 1) {
            return JSONResult.errorMsg("请至少选择1个文件");
        }

        ResourcesBO resourcesBO = new ResourcesBO();
        resourcesBO.setResourceName(resourceName);
        resourcesBO.setResourceCid(resourceCid);
        resourcesBO.setResourceUid(userName);
        resourcesBO.setResourceDesc(resourceDesc);
        resourcesBO.setResourceImage(coverImg);
        resourcesBO.setResourceUrl(files);

        return resourceService.addResource(resourcesBO);
    }

    @ApiOperation(value = "下载资源（已对接）", notes = "下载资源（已对接）", httpMethod = "GET")
    @GetMapping("/download")
    public void download(@RequestParam String path, HttpServletResponse response) throws IOException {
//        resourceService.download(path, response);
        fileUploadService.download(path, response);
    }






}
